/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ep.m004.ui.uiviews;

import com.vaadin.data.Property;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.ComponentContainer;
import com.vaadin.ui.Panel;
import com.vaadin.ui.Table;
import ep.libs.implementations.AbstractModule;
import ep.libs.implementations.AbstractView;
import ep.m001.ui.views.LoginView;
import ep.m002.module.M002Module;
import ep.m004.ui.uicontrollers.M004Controller;
import ep.m005.ui.views.M005View;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 *
 * @author atsi
 */
public class M004View extends AbstractView {

    private Object patientObject;
    private M004Controller controller;
    private String[] dosagesStrings;
    public static final String CAPTION_PRESCRIPTION = "Αριθμός Συνταγής";
    public static final String CAPTION_DURATION = "Λήψη έως";

    public M004View(AbstractModule module) {
        super(module);
    }

    public M004View(AbstractModule module, Object[] args) {
        super(module, args);
        this.patientObject = args[0];
    }

    @Override
    public ComponentContainer getView() {
        controller = new M004Controller(patientObject, module);
        initializeFormStrings();
        initializeComponents();
        return viewLayout;
    }

    public void initializeComponents() {
        Panel panel = new Panel();

        Table dosagesTable = new Table() {

            @Override
            protected String formatPropertyValue(Object rowId, Object colId, Property property) {
                if (colId.equals("duration")) {
                    Date date = (Date) property.getValue();
                    Calendar c = new GregorianCalendar();
                    c.setTime(date);
                    return c.get(c.DAY_OF_MONTH) + "/" + (c.get(c.MONTH) + 1) + "/" + c.get(c.YEAR);
                }
                return super.formatPropertyValue(rowId, colId, property);
            }
        };
//        dosagesTable.setColumnHeader("t006Prescription", CAPTION_PRESCRIPTION);
        dosagesTable.setColumnHeader("t009Medicine", M005View.CAPTION_MEDICINE);
        dosagesTable.setColumnHeader("quantity", M005View.CAPTION_QUANTITY);
//        dosagesTable.setColumnHeader("wayOfInput", M005View.CAPTION_WAY_OF_INPUT);
        dosagesTable.setColumnHeader("t013Frequency", M005View.CAPTION_FREQUENCY);
        dosagesTable.setColumnHeader("duration", CAPTION_DURATION);
//        dosagesTable.setColumnHeader("prescribed", M005View.CAPTION_PRESCRIBED);
        dosagesTable.setContainerDataSource(controller.getDosageDataSource());
        dosagesTable.setVisibleColumns(dosagesStrings);
        dosagesTable.setWidth("745px");

        Button backButton = new Button("Επιστροφή");
        backButton.addListener(new Button.ClickListener() {

            @Override
            public void buttonClick(ClickEvent event) {
                module.getApplication().changeModule(M002Module.class, new Object[]{LoginView.DOCTOR_TYPE, patientObject});
            }
        });

        panel.addComponent(dosagesTable);
        panel.addComponent(backButton);
        viewLayout.addComponent(panel);
    }

    private void initializeFormStrings() {
        dosagesStrings = new String[]{
                    //                    "t006Prescription",
                    "t009Medicine",
                    "quantity",
                    //                    "wayOfInput",
                    "t013Frequency",
                    "duration", //                    "prescribed"
                };


    }
}
